package appli;

import java.io.File;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.log4j.Logger;

import algutil.temps.Chronometre;

import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.MetadataException;

public class RollingFile extends ApplicationLC {

	public static final String VERSION = "1.0.0";

	public static final String VERSION_DATE = "29/04/2011";

	private static final Logger log = Logger.getLogger(RollingFile.class);

	private File rep = null;

	private String formatFileName = null;

	public int nbFichiersHistoMax;

	public RollingFile(String[] args) throws Exception {

		super(args);

		Chronometre chrono = new Chronometre();
		log.info("%%%%%%%%% RESUME %%%%%%%%%%");
		log.info("Repertoire      : " + rep.getPath());
		log.info("Nb fichiers max : " + nbFichiersHistoMax);
		log.info("Format fichier  : " + formatFileName);
		chrono.start();
		process();
		chrono.stop();
		log.info("Temps de traitement : " + chrono);
		log.info("%%%%%%%%%%% FIN %%%%%%%%%%%");

		arretExecution(0);
	}

	@Override
	protected void afficherUsage() {

		HelpFormatter formatter = new HelpFormatter();
		formatter.printHelp("RollingFile", options);
	}

	protected void genererCommandOptions() {

		options = new Options();
		Option nb = new Option("n", "nbFichiersMax", false,
				"Le nombre de fichiers max a conserver");
		Option f = new Option("f", "formatFileName", false,
				"Le format des fichiers");
		Option p = new Option("p", "path", true,
				"Repertoire ou sont contenu les fichiers");
		nb.setRequired(true);
		f.setRequired(true);
		p.setRequired(true);
		options.addOption(nb);
		options.addOption(f);
		options.addOption(p);
	}

	@Override
	protected boolean controlArguments(String[] args) {

		CommandLineParser parser = new PosixParser();
		try {
			CommandLine cmd = parser.parse(options, args);
			// nbFichiersHistoMax = Integer.parseInt(cmd.getOptionValue("n"));
			log.info("-n " + cmd.getOptionValue("-n"));
			rep = new File(cmd.getOptionValue("p"));
			if (!rep.exists() || !rep.isDirectory()) {
				log.error("Repertoire invalide : " + rep.getPath());
				return false;
			}
			formatFileName = cmd.getOptionValue("f");

		} catch (ParseException e) {
			log.error("Erreur lors du parse des arguments de la commande");
			return false;
		}

		return true;
	}

	private void process() throws JpegProcessingException, MetadataException {

	}

	public static void main(String[] args) throws Exception {

		new RollingFile(args);
	}
}
